import 'package:flutter/cupertino.dart';
import 'package:hitutor/Util/Global.dart';
import 'package:hitutor/Util/MyColor.dart';
import 'package:hitutor/Util/PageUtil.dart';
import 'package:hitutor/Widget/MyButton.dart';
import 'package:hitutor/Widget/MyDialog.dart';
import 'package:hitutor/Widget/MyImage.dart';
import 'package:hitutor/Widget/Star.dart';

import '../../../Util/HttpGo.dart';
import '../../Member/FavoriteTeacher/TeacherInfo.dart';
import 'TeacherInfo.dart';

class DialogChooseTeacher extends StatefulWidget {
  List<dynamic> teacherList;
  Function(int teacherId, String teacherName) onOrderCallback;

  DialogChooseTeacher({required this.teacherList, required this.onOrderCallback});

  @override
  State<StatefulWidget> createState() {
    return _DialogChooseTeacher();
  }
}

class _DialogChooseTeacher extends State<DialogChooseTeacher> {
  @override
  void initState() {
    super.initState();
  }


  getTeacherInfo(var teacherId){
    HttpGo(context).get("/Teacher/GetTeacherIntro?teacherId=$teacherId", {}, (data) {
      showWidgetDialog(context, TeacherInfo(data));
    }, (code,error) => showToast(error));
  }

  @override
  Widget build(BuildContext context) {
    return Container(
        decoration: Global.getWhiteBg(8),
        padding: EdgeInsets.symmetric(horizontal: 12, vertical: 8),
        child: Column(crossAxisAlignment: CrossAxisAlignment.end, children: [
          LocalImage("cross_gray.png", width: 20, padding: EdgeInsets.all(8), onTap: () {
            PageUtil().back(context);
          }),
          Expanded(
              child: ListView.builder(
                  itemCount: widget.teacherList.length,
                  itemBuilder: (BuildContext context, int index) {
                    return ItemTeacher(widget.teacherList[index], onTap: () {
                      getTeacherInfo(widget.teacherList[index]["teacherId"]);
                    }, onOrderTap: () {
                      PageUtil().back(context);
                      widget.onOrderCallback(widget.teacherList[index]["teacherId"], widget.teacherList[index]["teacherName"]);
                    });
                  }))
        ]));
  }
}

class ItemTeacher extends StatelessWidget {
  dynamic teacherData;
  Function()? onTap;
  Function()? onOrderTap;

  ItemTeacher(this.teacherData, {this.onTap, this.onOrderTap});

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
        onTap: onTap,
        child: Container(
            color: Color(0xffeaf6ff),
            margin: EdgeInsets.symmetric(vertical: 6),
            child: Row(children: [
              Container(height: 80, width: 3, color: Color(0xff2e4f90)),
              NetImage(teacherData["photo"], height: 60, width: 60, radius: 40, padding: EdgeInsets.symmetric(horizontal: 8)),
              Expanded(
                  child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
                Row(children: [
                  Text("${teacherData["teacherName"]}",
                      style: TextStyle(fontSize: 18, color: MyColor.BlueTxt, fontWeight: FontWeight.bold)),
                  teacherData["webcam"] ? Container() : LocalImage("video-off.png", width: 24, padding: EdgeInsets.symmetric(horizontal: 4))
                ]),
                Container(height: 9),
                Row(children: [
                  Expanded(child: Star(starWidth: 18, diverWidth: 1, value: teacherData["teacherGrade"] + 0.0,canChoose: false)),
                  TextBtn(" 立即訂課　", height: 25, style: TextStyle(fontSize: 14, color: MyColor.BlueTxt), onTap: onOrderTap)
                ])
              ])),
            ])));
  }
}
